package online.foxnull2.foxmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import online.foxnull2.common.constant.ProductConstant;
import online.foxnull2.foxmall.product.entity.AttrAttrgroupRelationEntity;
import online.foxnull2.foxmall.product.entity.AttrEntity;
import online.foxnull2.foxmall.product.entity.AttrGroupEntity;
import online.foxnull2.foxmall.product.entity.CategoryEntity;
import online.foxnull2.foxmall.product.mapper.AttrMapper;
import online.foxnull2.foxmall.product.service.AttrAttrgroupRelationService;
import online.foxnull2.foxmall.product.service.AttrGroupService;
import online.foxnull2.foxmall.product.service.AttrService;
import online.foxnull2.foxmall.product.service.CategoryService;
import online.foxnull2.foxmall.product.utils.PageUtils;
import online.foxnull2.foxmall.product.utils.Query;
import online.foxnull2.foxmall.product.vo.AttrRespVo;
import online.foxnull2.foxmall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrMapper, AttrEntity> implements AttrService {

    private final AttrAttrgroupRelationService attrAttrgroupRelationService;
    private final CategoryService categoryService;
    private AttrGroupService attrGroupService;

    @Autowired
    public AttrServiceImpl(AttrAttrgroupRelationService attrAttrgroupRelationService,
                           CategoryService categoryService) {
        this.attrAttrgroupRelationService = attrAttrgroupRelationService;
        this.categoryService = categoryService;
    }

    @Autowired
    public void setAttrGroupService(AttrGroupService attrGroupService) {
        this.attrGroupService = attrGroupService;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveCascade(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        attrEntity
                .setAttrName(attrVo.getAttrName())
                .setSearchType(attrVo.getSearchType())
                .setValueType(attrVo.getValueType())
                .setIcon(attrVo.getIcon())
                .setValueSelect(attrVo.getValueSelect())
                .setAttrType(attrVo.getAttrType())
                .setEnable(attrVo.getEnable())
                .setCatelogId(attrVo.getCatelogId())
                .setShowDesc(attrVo.getShowDesc());
//        BeanUtils.copyProperties(attr, attrEntity);
        this.save(attrEntity);
        if (Objects.equals(ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode(), attrVo.getAttrType()) && !ObjectUtils.isEmpty(attrVo.getAttrGroupId())) {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity
                    .setAttrId(attrEntity.getAttrId())
                    .setAttrGroupId(attrVo.getAttrGroupId())
                    .setAttrSort(0);
            attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
        }
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, String attrType, Long catelogId) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();

        if (!ObjectUtils.isEmpty(attrType)) {
            queryWrapper.eq("attr_type",
                    ProductConstant.AttrEnum.ATTR_TYPE_BASE.getMsg().equalsIgnoreCase(attrType) ?
                            ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() :
                            ProductConstant.AttrEnum.ATTR_TYPE_SALE.getMsg().equalsIgnoreCase(attrType) ?
                                    ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode() : 2);
        }
        if (!ObjectUtils.isEmpty(catelogId) && 0 != catelogId) {
            queryWrapper.eq("catelog_id", catelogId);
        }
        if (!ObjectUtils.isEmpty(params)) {
            Object key = params.get("key");
            if (!ObjectUtils.isEmpty(key)) {
                queryWrapper.and((wrapper) -> wrapper.eq("attr_id", key.toString()).or().like("attr_name", key.toString()));
            }
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );

        List<AttrRespVo> respVoList = page.getRecords().stream().map(attrEntity ->
        {
            AttrRespVo attrRespVo = new AttrRespVo()
                    .setAttrId(attrEntity.getAttrId())
                    .setAttrName(attrEntity.getAttrName())
                    .setSearchType(attrEntity.getSearchType())
                    .setValueType(attrEntity.getValueType())
                    .setIcon(attrEntity.getIcon())
                    .setValueSelect(attrEntity.getValueSelect())
                    .setAttrType(attrEntity.getAttrType())
                    .setEnable(attrEntity.getEnable())
                    .setShowDesc(attrEntity.getShowDesc());

            CategoryEntity categoryEntity = categoryService.getById(attrEntity.getCatelogId());
            if (!ObjectUtils.isEmpty(categoryEntity)) {
                attrRespVo.setCatelogName(categoryEntity.getName());
            }

            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.getOne(
                    new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId())
            );
            if (!ObjectUtils.isEmpty(attrAttrgroupRelationEntity)) {
                AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId());
                if (!ObjectUtils.isEmpty(attrGroupEntity)) {
                    attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }
            return attrRespVo;
        }).collect(Collectors.toList());

        return new PageUtils(page).setList(respVoList);
    }

    @Transactional
    @Override
    public void updateCascade(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        attrEntity
                .setAttrId(attrVo.getAttrId())
                .setAttrName(attrVo.getAttrName())
                .setSearchType(attrVo.getSearchType())
                .setValueType(attrVo.getValueType())
                .setIcon(attrVo.getIcon())
                .setValueSelect(attrVo.getValueSelect())
                .setAttrType(attrVo.getAttrType())
                .setEnable(attrVo.getEnable())
                .setCatelogId(attrVo.getCatelogId())
                .setShowDesc(attrVo.getShowDesc());
        this.updateById(attrEntity);
        if (Objects.equals(ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode(), attrVo.getAttrType())) {
            attrAttrgroupRelationService.update(new UpdateWrapper<AttrAttrgroupRelationEntity>()
                    .eq("attr_id", attrEntity.getAttrId()).set("attr_group_id", attrVo.getAttrGroupId()));
        }
    }

    @Override
    public List<AttrEntity> getRelationAttrs(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityList = attrAttrgroupRelationService.list(
                new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));

        if (!ObjectUtils.isEmpty(attrAttrgroupRelationEntityList)) {
            List<Long> attrIds = attrAttrgroupRelationEntityList.stream().map(AttrAttrgroupRelationEntity::getAttrId)
                    .collect(Collectors.toList());
            return this.listByIds(attrIds);
        }
        return null;
    }

    @Override
    public PageUtils getRelationNoattrs(Map<String, Object> params, Long attrGroupId) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>()
                .eq("attr_type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());

        Long catelogId = null;
        List<Long> attrIdList = new ArrayList<>();

        AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrGroupId);
        if (!ObjectUtils.isEmpty(attrGroupEntity)) {
            catelogId = attrGroupEntity.getCatelogId();
            List<AttrGroupEntity> attrGroupEntityList = attrGroupService.list(new QueryWrapper<AttrGroupEntity>()
                    .eq("catelog_id", catelogId));
            if (!ObjectUtils.isEmpty(attrGroupEntityList)) {
                List<Long> attrGroupIdList = attrGroupEntityList.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
                if (!ObjectUtils.isEmpty(attrGroupIdList)) {
                    List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityList = attrAttrgroupRelationService.list(
                            new QueryWrapper<AttrAttrgroupRelationEntity>()
                                    .in("attr_group_id", attrGroupIdList)
                    );
                    if (!ObjectUtils.isEmpty(attrAttrgroupRelationEntityList)) {
                        attrIdList = attrAttrgroupRelationEntityList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
                    }
                }
            }
        }
        if (!ObjectUtils.isEmpty(catelogId)) {
            queryWrapper.eq("catelog_id", catelogId);
        }
        if (!ObjectUtils.isEmpty(attrIdList)) {
            queryWrapper.notIn("attr_id", attrIdList);
        }
        if (!ObjectUtils.isEmpty(params)) {
            Object key = params.get("key");
            if (!ObjectUtils.isEmpty(key)) {
                queryWrapper.and(attrEntityQueryWrapper -> attrEntityQueryWrapper
                        .eq("attr_id", key.toString()).like("attr_name", key.toString()));
            }
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

}